<template>
    <div class="apiCacheContainer">
        <div class="searchContainer">
            <RoleSearch />
        </div>
        <div class="insertContainer">
            <el-button type="primary" plain size="small" icon="PlusOutlined" @click="addRoleInfo()">添 加</el-button>
        </div>
        <div>
            <el-table
                :data="roleDataList.dataList"
                style="width: 100%; margin-bottom: 20px; margin-top: 20px;"
                row-key="id"
                border
                :stripe="true"
                :show-overflow-tooltip="true"
                class="tableContainer"
                max-height="70vh"
            >
                <el-table-column prop="id" label="角色ID" />
                <el-table-column prop="roleCode" label="角色Code" />
                <el-table-column prop="roleName" label="角色名称" />
                <el-table-column prop="roleDescription" label="角色描述" />
                <el-table-column align="center">
                    <template #header>
                        <span>操 作</span>
                    </template>
                    <template #default="scope">
                        <el-button size="small" type="success" @click="handleUpdate(scope.$index, scope.row)">修 改</el-button>
                        <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删 除</el-button>
                        <el-button type="primary" size="small" @click="handleCopy(scope.$index, scope.row)">复 制</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                v-model:current-page="roleDataList.pageIndex"
                v-model:page-size="roleDataList.pageSize"
                :small="false"
                :background="true"
                layout="prev, pager, next, jumper, sizes, total"
                :total="roleDataList.recordCount"
                @change="changePage"
                :page-sizes="[10, 20, 30, 50, 100, 200, 400]"
                style="float: right"
            />
        </div>

        <div>
            <RoleAddOrUpdateDialog :roleDataForm="roleDataForm" :title="title"/>
        </div>

        <div>
            <RoleDeleteDialog :roleDataForm="roleDataForm"/>
        </div>
    </div>
</template>

<script>
import RoleSearch from "@/components/userRoleManage/roleManage/RoleSearch";
import RoleAddOrUpdateDialog from "@/components/userRoleManage/roleManage/RoleAddOrUpdateDialog";
import RoleDeleteDialog from "@/components/userRoleManage/roleManage/RoleDeleteDialog";
import {createTag} from "@/utils/tagCreate";
import {roleInfoQuery} from "@/request/role";

export default {
    name: "Role",
    components: {
        RoleSearch,
        RoleAddOrUpdateDialog,
        RoleDeleteDialog
    },
    data() {
        return {
            roleDataForm: {
                id: '',
                roleCode: '',
                roleName: '',
                roleDescription: ''
            },
            title: ''
        }
    },
    computed: {
        roleDataList() {
            return this.$store.state.roleDataList;
        },
        roleSearchData() {
            return this.$store.state.roleSearchData;
        }
    },
    methods: {
        addRoleInfo() {
            this.title = "添加角色";
            this.roleDataForm = {
                id: '',
                roleCode: '',
                roleName: '',
                roleDescription: ''
            }
            this.$store.commit('changeRoleAddOrUpdateDialogVisible', true);
        },
        handleUpdate(index, row) {
            this.title = "修改角色";
            this.roleDataForm = {
                id: row.id,
                roleCode: row.roleCode,
                roleName: row.roleName,
                roleDescription: row.roleDescription
            }
            this.$store.commit('changeRoleAddOrUpdateDialogVisible', true);
        },
        handleCopy(index, row) {
            this.title = "添加角色";
            this.roleDataForm = {
                id: '',
                roleCode: row.roleCode,
                roleName: row.roleName,
                roleDescription: row.roleDescription
            }
            this.$store.commit('changeRoleAddOrUpdateDialogVisible', true);
        },
        handleDelete(index, row) {
            this.roleDataForm = {
                id: row.id,
                roleCode: row.roleCode,
                roleName: row.roleName,
                roleDescription: row.roleDescription
            }
            this.$store.commit('changeRoleDeleteDialogVisible', true);
        },
        changePage(index, size) {
            this.roleSearchData.pageInfo = {
                pageIndex: index,
                pageSize: size
            };
            roleInfoQuery(this.roleSearchData).then(res => {
                this.$store.commit('changeRoleDataList', res.data)
            })
        }
    },
    mounted() {
        createTag(this.$route, this.$store)
    },
    created() {
        roleInfoQuery(this.roleSearchData).then(res => {
            if (res.code === '200') {
                this.$store.commit('changeRoleDataList', res.data)
            }
        })
    }
}
</script>

<style scoped lang="less">
.apiCacheContainer {
    width: 98%;
    height: 100%;
    margin: 0.2%;
    .searchContainer {
        width: 100%;
        margin-top: 1%;
    }
    .tableContainer {
        margin-left: 1%;
    }
    .insertContainer {
        margin-left: 1%;
        margin-top: 0.7%;
    }
    .refreshContainer {
        margin-top: 5%;
        //去除光标
        caret-color: transparent;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
}
</style>